package com.astronomy.gathering.service;

import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.astronomy.gathering.mapper.GatheringMapper;
import com.astronomy.gathering.pojo.Gathering;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class GatheringService {

    @Autowired
    private GatheringMapper gatheringMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     * @return
     */
    public List<Gathering> findAll(){
        return gatheringMapper.selectAll();
    }

    /**
     * 根据ID查询
     *   @Cacheable 存key
     *   (value = "gathering",key = "")
     *   value 全局的标识 缓存名称
     *   key 需要存到缓存的key
     * @param id
     * @return
     */
    @Cacheable(value = "gathering",key = "#id")
    public Gathering findById(String id){
        return  gatheringMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param gathering
     */
    public void add(Gathering gathering){
        gathering.setId(idWorker.nextId()+"");
        gatheringMapper.insert(gathering);
    }

    /**
     * 修改
     * @CacheEvict 从缓存里删key
     * @param gathering
     */
    @CacheEvict(value = "gathering",key = "#gathering.id")
    public void update(Gathering gathering){
        gatheringMapper.updateByPrimaryKey(gathering);
    }

    /**
     * 删除
     * @param id
     */
    @CacheEvict(value = "gathering",key = "#id")
    public void delete(String id){
        gatheringMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public PageResult<Gathering> findPage(int page, int size){
        PageHelper.startPage(page,size);
        List<Gathering> gatherings = gatheringMapper.selectAll();
        PageInfo<Gathering> gatheringPageInfo = new PageInfo<>(gatherings);
        return new PageResult<>(gatheringPageInfo.getTotal(),gatheringPageInfo.getList());
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Gathering> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return gatheringMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Gathering.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 编号
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 活动名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 大会简介
            if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                criteria.andLike("summary","%"+searchMap.get("summary")+"%");
           	}
            // 详细说明
            if(searchMap.get("detail")!=null && !"".equals(searchMap.get("detail"))){
                criteria.andLike("detail","%"+searchMap.get("detail")+"%");
           	}
            // 主办方
            if(searchMap.get("sponsor")!=null && !"".equals(searchMap.get("sponsor"))){
                criteria.andLike("sponsor","%"+searchMap.get("sponsor")+"%");
           	}
            // 活动图片
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
           	}
            // 举办地点
            if(searchMap.get("address")!=null && !"".equals(searchMap.get("address"))){
                criteria.andLike("address","%"+searchMap.get("address")+"%");
           	}
            // 是否可见
            if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                criteria.andLike("state","%"+searchMap.get("state")+"%");
           	}
            // 城市
            if(searchMap.get("city")!=null && !"".equals(searchMap.get("city"))){
                criteria.andLike("city","%"+searchMap.get("city")+"%");
           	}


        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public PageResult<Gathering> findPage(Map<String, Object> searchMap, int page, int size){

        Example example = createExample(searchMap);
        PageHelper.startPage(page,size);
        List<Gathering> gatherings = gatheringMapper.selectByExample(example);
        PageInfo<Gathering> gatheringPageInfo = new PageInfo<>(gatherings);
        return new PageResult<>(gatheringPageInfo.getTotal(),gatheringPageInfo.getList());
    }

}
